/*
 * @Author: zxc
 * @Date: 2020-07-23 13:05:09
 * @LastEditTime: 2020-09-08 12:07:00
 * @LastEditors: zxc
 * @Description: 创建一个store对象
 *
 */
import { createStore, applyMiddleware, compose } from "redux";
import { routinePromiseWatcherSaga } from "redux-saga-routines";
import createSagaMiddleware from "redux-saga";
import { createBrowserHistory } from "history";
import { routerMiddleware } from "connected-react-router";
// import logger from "redux-logger";
import thunk from "redux-thunk";
import rootReducer from "./reducer";
import rootSaga from "./saga/sagas";

export const history = createBrowserHistory();

//saga中间件
const sagaMiddleware = createSagaMiddleware();

//创建一个增强器 => 返回一个高阶函数
//配置redux dev tools
const composeEnhancer = (window as any).__REDUX_DEVTOOLS_EXTENSION_COMPOSE_
  ? (window as any).__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({})
  : compose;

//createstore参数 ： reducer,[initialState]/初始化,[Enhancer]/增强器
const store = createStore(
  rootReducer(history),
  composeEnhancer(
    applyMiddleware(
      routerMiddleware(history), 
      sagaMiddleware, 
      thunk,
      // logger
    )
  )
);

sagaMiddleware.run(rootSaga);

/*
* 为了方便在组件内部或者某个方法内部处理回调
* 我们需要触发一个promise类型的action 所以需要routinePromiseWatcherSaga这个方法
* 去帮助我们监听我们触发的每一个promise的action，
实际上，routinePromiseWatcherSaga这个方法在内部，只是帮我们实现了一个返回promise的功能
* */
sagaMiddleware.run(routinePromiseWatcherSaga);

export default store;
